package com.yidu.t299.controller.zzy;

import com.yidu.t299.domain.zzy.Record;
import com.yidu.t299.domain.zzy.TeacherRemark;
import com.yidu.t299.service.zzy.RecordaBiz;
import com.yidu.t299.service.zzy.TeacherRemarkBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:周子怡
 * @Date:2023/03/08/9:04
 * @Description:
 */
@RestController
public class TeacherRemarkController {
    @Autowired
    private TeacherRemarkBiz teacherRemarkBiz;

    @Autowired
    private RecordaBiz recordBiz;
    /**
     * 初始化操作，跳转并保存下拉框数据(实现动态下拉框功能)
     */
    @RequestMapping("teacherRemark/init")
    public ModelAndView saveSelect(){
        List<Record> recordList = recordBiz.selectAllGetRecord();//得到所有课程信息
        System.out.println(recordList);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("teacherRemarkCRUD");//跳转的页面
        mv.addObject("recordList",recordList); //保存课程信息
        return mv;
    }

    /**
     * 分页查询显示数据在表格中
     *
     * @param limit 页码
     * @param page  每页的行数
     * @param teacherRemark   接收多条件搜索的表单数据对象
     * @return 映射集合对象
     */
    @GetMapping("teacherRemark/restful_teacherRemark")
    public Map<String, Object> findAll(int limit, int page, TeacherRemark teacherRemark) {
        System.out.println("======search by page: obj is:===========");
        System.out.println(teacherRemark);
        // 根据school对象得到条件字符串
        String condition = teacherRemarkBiz.getCondition(teacherRemark);
        System.out.println("condition:"+condition);
        // 按条件统计总记录行数
        int count = teacherRemarkBiz.selectCount(condition);
        // 按条件分页查询
        List<TeacherRemark> teacherRemarkList =teacherRemarkBiz.selectByPage(limit, page-1, condition);

        System.out.println(teacherRemarkList);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为校区对象集合的映射
        mapData.put("data", teacherRemarkList);
        //把映射集合响应到客户端
        return mapData;
    }

    /**
     * 添加一个新校区
     *
     * @param teacherRemark 校区对象：其前要跟注解 @RequestBody，因为前端页面提交过来的数据就是JSON格式的字符串
     * @return 提示信息字符串
     */
    @PostMapping("teacherRemark/restful_teacherRemark")
    public String addEmp(@RequestBody TeacherRemark teacherRemark) {
        System.out.println(teacherRemark);
        //得到学员编号和课程编号，判断是否为空，不为空进行操作
        if(teacherRemark!=null){
            if(teacherRemark.getSchoolName()!=null && teacherRemark.getClassName()!=null&& teacherRemark.getRecordName()!=null){
                System.out.println("进来了");
                System.out.println(teacherRemark.getClassName()+" -- "+teacherRemark.getSchoolName()+" -- "+teacherRemark.getRecordName());
                //查询出学员编号和课程编号，存入grade对象中
                teacherRemark.setClassId(teacherRemarkBiz.selectByClassName(teacherRemark.getClassName()));
                teacherRemark.setLessonId(teacherRemarkBiz.selectByLessonName(teacherRemark.getLessonName()));
                teacherRemark.setTeacherId(teacherRemarkBiz.selectByRecordName(teacherRemark.getRecordName()));
                System.out.println(teacherRemark);
                //调用业务层实现新增成绩，并返回正确与否字符串
                return teacherRemarkBiz.insert(teacherRemark) > 0 ? "add-success" : "add-failure";
            }
        }
        //grade为空，直接返回错误信息
        return "add-failure";
    }

    /**
     * 修改成绩信息
     *
     * @param teacherRemark 成绩对象
     * @return 提示信息字符串
     */
    @PutMapping("teacherRemark/restful_teacherRemark")
    public String updateEmp(@RequestBody TeacherRemark teacherRemark) {
        //得到学员编号和课程编号，判断是否为空，不为空进行操作
        if(teacherRemark!=null){
            if(teacherRemark.getLessonName()!=null && teacherRemark.getClassName()!=null&& teacherRemark.getRecordName()!=null){
                //查询出学员编号和课程编号，存入grade对象中
                teacherRemark.setClassId(teacherRemarkBiz.selectByClassName(teacherRemark.getClassName()));
                teacherRemark.setLessonId(teacherRemarkBiz.selectByLessonName(teacherRemark.getLessonName()));
                teacherRemark.setTeacherId(teacherRemarkBiz.selectByRecordName(teacherRemark.getRecordName()));
                //调用业务层实现修改成绩，并返回正确与否字符串
                return teacherRemarkBiz.updateByPrimaryKey(teacherRemark) > 0 ? "update-success" : "update-failure";
            }
        }
        //grade为空，直接返回错误信息
        return "update-failure";
    }

    /**
     * 根据校区编号删除校区
     *
     * @param remarkId 校区编号
     * @return 提示信息字符串
     */
    @DeleteMapping("teacherRemark/restful_teacherRemark/{remarkId}")
    public String deleteEmpById(@PathVariable("remarkId") int remarkId) {
        //调用业务层实现删除校区，并返回正确与否字符串
        return teacherRemarkBiz.deleteByPrimaryKey(remarkId) > 0 ? "delete-success" : "delete-failure";
    }

    /**
     * 根据多个校区编号批量删除校区
     * 此方法使用put请求替代delete，put可以带请求体
     *
     * @param remarkIdStr 多个校区编号字符串
     * @return 提示信息字符串
     */
    @PutMapping("teacherRemark/batchDel")
    public String deleteEmpByManyId(@RequestParam String remarkIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = remarkIdStr.split(",");
        //循环按校区编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (teacherRemarkBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "delete-failure";
            }
        }
        return "delete-success";
    }
}
